//
//  DownloadViewController.m
//  A1.0
//
//  Created by lanou3g on 15-4-9.
//  Copyright (c) 2015年 ccandxx. All rights reserved.
//

#import "DownloadViewController.h"
#import "UIImageView+WebCache.h"
#import "PicDetailModel.h"
#import "UIImageView+adapt.h"
@interface DownloadViewController ()<UIScrollViewDelegate>

@property(nonatomic,strong) UIImageView *imV;

@property (nonatomic ,retain)UIScrollView *scroll;


@end

@implementation DownloadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = [UIColor blackColor];
    

    [self p_setupView];
    
}

- (void)p_setupView
{
    
    //创建scrollview
    self.scroll = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    //设置属性
    self.scroll.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:1];
    //决定内容区域的大小
    
    self.scroll.contentSize =CGSizeMake(self.view.frame.size.width, self.view.frame.size.width*self.picDM.pic_h/self.picDM.pic_w);
    
    
    //返回顶部
    self.scroll.scrollsToTop = YES;
    
    //是否有回弹效果
    self.scroll.bounces =YES;
    
    //是否能够滚动
    self.scroll.scrollEnabled = YES;
    
    //垂直滚动条
    self.scroll.showsVerticalScrollIndicator = YES;
    
    
    //水平是否回弹
    self.scroll.alwaysBounceHorizontal = NO;
    //垂直是否回弹
    self.scroll.alwaysBounceVertical = YES;
    
    
    //设置属性
    self.scroll.alwaysBounceHorizontal = YES;
    self.scroll.alwaysBounceVertical = YES;
    //缩放属性设置
    //最小缩放比例
    self.scroll.minimumZoomScale = 0.5;
    //最大缩放比例
    self.scroll.maximumZoomScale = 2.0;
    
    //缩放反弹
    self.scroll.bouncesZoom = YES;
    
    
    
    self.scroll.delegate = self;
    
    
    
    if (self.view.frame.size.width*self.picDM.pic_h/self.picDM.pic_w > [UIScreen mainScreen].bounds.size.height)
    {
        _imV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.picDM.pic_h * self.view.frame.size.width / self.picDM.pic_w)];
    }
    else
    {
        _imV = [[UIImageView alloc] initWithFrame:CGRectMake(0, (self.view.frame.size.height - self.picDM.pic_h  * self.view.frame.size.width / self.picDM.pic_w ) / 2, self.view.frame.size.width, self.picDM.pic_h * self.view.frame.size.width / self.picDM.pic_w)];
    }
    
    
    _imV.userInteractionEnabled = YES;
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(picTapped:)];
    [_imV addGestureRecognizer:singleTap];
    
    
    [_imV sd_setImageWithURL:[NSURL URLWithString:self.picDM.pic] placeholderImage:[UIImage imageNamed:@"cat.png"]];
    
    [_imV setContentScaleFactor:[[UIScreen mainScreen] scale]];
    _imV.contentMode =  UIViewContentModeScaleAspectFill;
    _imV.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    _imV.clipsToBounds  = YES;
    
    
    
    UIImageView *down = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 20, self.view.frame.size.height - 40, 40, 40)];
    
    down.image = [UIImage imageNamed:@"down.png"];
    
    
    down.userInteractionEnabled = YES;
    UITapGestureRecognizer *downTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(saveImageToAlbum:)];
    [down addGestureRecognizer:downTap];
    
    //将imageView添加到scrollView上
    [self.scroll addSubview:_imV];
    
    [self.view addSubview:self.scroll];
    
    [self.view addSubview:down];

}





- (void)picTapped:(UITapGestureRecognizer *)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)saveImageToAlbum:(id)sender {
    UIImageWriteToSavedPhotosAlbum(self.imV.image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
}


- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (!error) {
        UIAlertView *warming = [[UIAlertView alloc]initWithTitle:@"保存成功" message:@"" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [warming show];
       
    }else
    {
        UIAlertView *warming = [[UIAlertView alloc]initWithTitle:@"保存失败" message:@"请查看隐私相册权限" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [warming show];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//缩放中执行
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
 
    
    //缩放之后图片回到中心
    //获取屏幕的大小
    CGSize viewSize = self.view.frame.size;
    
    //获取图片位置和大小
    CGRect imageViewFrame = _imV.frame;
    
    //获取内容大小contentsize
    CGSize contentSize = scrollView.contentSize;
    
    //如果contentsize的宽度小于屏幕的宽度
    if (contentSize.width<viewSize.width) {
        imageViewFrame.origin.x = (viewSize.width-contentSize.width)/2;
    }else
    {
        imageViewFrame.origin.x = 0;
    }
    //如果contentsize的高度小于屏幕的高度
    if (contentSize.height <viewSize.height) {
        imageViewFrame.origin.y = (viewSize.height - contentSize.height)/2;
    }else
    {
        imageViewFrame.origin.y = 0;
    }
    //更新
    _imV.frame = imageViewFrame;
}


//指定缩放的view
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imV ;
    
}
@end
